package com.itboye.ihomebank.zjtlcb.lnd.livenessdetector.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;

import com.itboye.ihomebank.R;
import com.itboye.ihomebank.activity.qianyue.OrderDetailActivity;
import com.itboye.ihomebank.base.MyApplcation;
import com.itboye.ihomebank.zjtlcb.lnd.livenessdetector.utils.OkHttpRequestUtil;

import org.json.JSONObject;

public class MainLiveActivity extends Activity implements View.OnClickListener{
    public static final String TAG = MainLiveActivity.class.getSimpleName();

    private String responseToken = "";//开发者验证Token

    private String appID = "";//APP秘钥
    private String randomNum = "";//APP秘钥
    private String seqNO = "";
    private String appSecretKey = "";
    private String sign = "";
    private String requestCode1 = "";//开发者请求代码
    private String requestURL = "";//后台地址
    private String requestAccessorJSon = "";       //访问者报文

    private MyApplcation mApplication;

    private ImageButton btStartDec;
    private ImageButton btReturn;
    private EditText etBussSeqNo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mainlive);

        initViews();//初始化布局
        initControllers();//初始化控件逻辑

        AccessorThread accessorThread = new AccessorThread();
        accessorThread.start();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.bt_start_detc:
                mApplication = (MyApplcation)getApplication();
                mApplication.setBussSeqNo(etBussSeqNo.getText().toString());
                Log.e(TAG, "该笔bussSeqNo为："+mApplication.getBussSeqNo());

                Intent intent = new Intent(MainLiveActivity.this, DetecActionActivity.class);
                startActivity(intent);
                finish();
                break;
            case R.id.bt_return:
                finish();
                break;
            default:
                break;
        }
    }

    private void initControllers() {
        btStartDec.setOnClickListener(this);
        btReturn.setOnClickListener(this);
    }

    public void initViews(){
        btStartDec = (ImageButton)findViewById(R.id.bt_start_detc);
        btReturn = (ImageButton)findViewById(R.id.bt_return);
        etBussSeqNo = (EditText) findViewById(R.id.et_bussSeqNo);
        etBussSeqNo.setText(getIntent().getStringExtra("bussSeqNo="));

    }

    //创建子线程
    class AccessorThread extends Thread{
        @Override
        public void run() {
            try {
                MyApplcation requestBean = new MyApplcation();
                appID = requestBean.appID;
                randomNum = requestBean.randomNum;
                seqNO = requestBean.seqNO;
                appSecretKey = requestBean.appSecretKey;
                sign = requestBean.sign;
                requestCode1 = requestBean.requestCode1;
                requestURL = requestBean.requestURL;
                requestAccessorJSon =   //访问者获取Token报文
                "{" +
                        "\"appID\": "+"\""+appID+"\"," +
                        "\"random\": "+"\""+randomNum+"\"," +
                        "\"seqNO\": "+"\""+seqNO+"\"," +
                        "\"sign\":"+"\""+sign+"\"" +
                        "}";
                Log.e(TAG, "子线程" + Thread.currentThread() + "开始执行访问者获取返回报文");

                OkHttpRequestUtil OHRUtil = new OkHttpRequestUtil();
                Log.e(TAG, "已新建OkHttpRequestUtil,传递的参数分别为："+requestAccessorJSon+"-"+requestURL+"-"+requestCode1+"-"+TAG);

                String accessorResponse = OHRUtil.okHttpRequest(requestAccessorJSon, requestURL, requestCode1,TAG);
                //解析json
                JSONObject mJsonObject = new JSONObject(accessorResponse);
                responseToken = mJsonObject.optString("appAccessToken");
                //存数据
                SharedPreferences.Editor editor = getSharedPreferences("mainActivity", Context.MODE_PRIVATE).edit();
                editor.putString("appAccessToken",responseToken)
                      .commit();
                Log.e(TAG, requestCode1+"获取金融云返回访问者Token："+responseToken);
                super.run();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }
}
